package com.xdtech.sys.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.sys.service.TaskService;
import com.xdtech.sys.vo.TaskItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-05-10 23:05:41
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class TaskController {
	@Autowired
	private TaskService taskService;
	@RequestMapping(value="/task.do",params = "task")
	public ModelAndView skipTask() {
		return new ModelAndView("sys/task/task_ftl");
	}
	private Log log = LogFactory.getLog(TaskController.class);
	
	@RequestMapping(value="/task.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(TaskItem item,Pagination pg) {
		Map<String, Object> results =  taskService.loadPageDataByConditions(pg,item,"findTaskByCondition");
		return results;
	}
	
	@RequestMapping(value="/task.do",params = "editTask")
	public ModelAndView editTask(HttpServletRequest request,Long taskId) {
		if (taskId!=null) {
			request.setAttribute("taskItem", taskService.loadTaskItem(taskId));
		}
		return new ModelAndView("sys/task/editTask_ftl");
	}
	
	@RequestMapping(value="/task.do",params = "saveTask")
	@ResponseBody
	public ResultMessage saveTask(TaskItem item) {
		ResultMessage r = new ResultMessage();
		if (taskService.saveOrUpdateTask(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/task.do",params = "stopTask")
	@ResponseBody
	public ResultMessage stopTask(String ids,String flag) {
		ResultMessage r = new ResultMessage();
		try
		{
			if (StringUtils.isNotEmpty(ids)) {
				String[] tempIds = ids.split(",");
				List<Long> taskIds = new ArrayList<Long>();
				for (String id : tempIds) {
					taskIds.add(Long.valueOf(id));
				}
				taskService.stopOrStartTasks(taskIds,flag);
				r.setSuccess(true);
			}else {
				r.setSuccess(false);
			}
		}
		catch (Exception e)
		{
			log.error("任务处理异常",e);
			r.setSuccess(false);
			r.setMsg("处理异常");
		}
		
		return r;
	}
	
	@RequestMapping(value="/task.do",params = "deleteTaskItems")
	@ResponseBody
	public ResultMessage deleteTaskItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> taskIds = new ArrayList<Long>();
			for (String id : tempIds) {
				taskIds.add(Long.valueOf(id));
			}
			taskService.deleteTaskInfo(taskIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
